<template>
  <div class="table-box">
    <ProTable ref="proTable" title="用户列表" :columns="columns" :request-api="getUserList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增用户 </el-button>
      </template>
      <!-- Expand -->
      <template #expand="scope">
        {{ scope.row }}
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-dropdown trigger="hover">
          <el-button type="primary">
            操作菜单
            <el-icon><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <el-button v-auth="'view'" type="primary" link :icon="View" @click="openDrawer('查看', scope.row)">
                  查看
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
                  编辑
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)">
                  删除
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'reSet'" type="primary" link :icon="Refresh" @click="resetPassword(scope.row)">
                  重置密码
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </ProTable>
    <UserDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="ts" name="userManager">
import { ref } from "vue";
import { System } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import UserDialog from "@/views/system/components/UserDialog.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { CirclePlus, EditPen, View, Delete, Refresh } from "@element-plus/icons-vue";
import { getUserList, createUser, editUser, deleteUser, resetPas } from "@/api/modules/system";
import { genderType, accountType, accountStatus } from "@/utils/serviceDict";

const proTable = ref<ProTableInstance>();

// 表格配置项
const columns: ColumnProps<System.ResUserList>[] = [
  { type: "index", label: "序号", width: 60 },
  { sortable: true, prop: "userName", minWidth: 120, label: "账号", align: "left" },
  { sortable: true, prop: "nickName", label: "昵称", minWidth: 120, align: "left", search: { el: "input" } },
  {
    sortable: true,
    prop: "sex",
    label: "性别",
    minWidth: 100,
    enum: genderType,
    search: { el: "select" }
  },
  { sortable: true, prop: "userType", label: "账户类型", minWidth: 120, enum: accountType, align: "left" },
  { sortable: true, prop: "state", label: "状态", minWidth: 100, enum: accountStatus, align: "left" },
  { sortable: true, prop: "defaultPassWord", label: "默认密码", minWidth: 100, align: "left" },
  { prop: "operation", label: "操作", fixed: "right", width: 128 }
];

// 删除
const deleteAccount = async (params: System.ResUserList) => {
  await useHandleData(deleteUser, { id: params.id }, `删除【${params.nickName}】用户`);
  proTable.value?.getTableList();
};

const resetPassword = async (params: System.ResUserList) => {
  await useHandleData(resetPas, { id: params.id }, `重置【${params.nickName}】密码`);
  // proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof UserDialog> | null>(null);
const openDrawer = (title: string, row: Partial<System.ResUserList> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: title === "新增" ? createUser : title === "编辑" ? editUser : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
